package cmd

import (
	"log"
	"regexp"
)

// 存储校验项的映射 key: 是抽象拼接后的路径，value: 是文件的hash值
type CheckItems map[string]string

// 定义核心校验规则map
// 如果路径以/结尾则表示获取目录下的所有文件, 否则表示获取通配符或者指定的文件
// 如果值为false则表示排除该路径
var CoreCheckItems = map[string]map[string]bool{
	"jointos": {
		"platform/bin/*.jar":                                 true,
		"platform/lib/":                                      true,
		"platform/webapps/jointos/WEB-INF/lib/":              true,
		"platform/webapps/jointframe/":                       true,
		"platform/webapps/jointframe/config.env.js":          false,
		"platform/webapps/jointframe/web/config.env.js":      false,
		"platform/webapps/jointos/WEB-INF/platform/plugins/": true,
	},
	"amonline": {
		"platform/bin/CATALINA_PID":                                  false,
		"platform/lib/":                                              true,
		"platform/webapps/amOnline/static/":                          true,
		"platform/webapps/amOnline/zdjk-company/":                    true,
		"platform/webapps/amOnline/zdjk-company/config.env.js ":      false,
		"platform/webapps/amOnline/zdjk-company-base/config.env.js ": false,
		"platform/webapps/amOnline/zdjk-company-base/":               true,
		"platform/webapps/amOnline/WEB-INF/lib/":                     true,
		"platform/webapps/amOnline/WEB-INF/platform/plugins/":        true,
	},
	"AutoMonitorManagerQY": {
		"lib/": true,
	},
	"AutoMonitorManager": {
		"lib/": true,
	},
	"AutoMonitorRefreshServer": {
		"lib/": true,
	},
	"AutoMonitorServer": {
		"lib/": true,
	},
	"jointcloud": {
		"jointdss/lib/":    true,
		"jointmq/lib/":     true,
		"jointmq/webapps/": true,
		"platform/webapps/jointcloud/WEB-INF/lib/":              true,
		"platform/webapps/jointcloud/WEB-INF/platform/plugins/": true,
	},
	"Communicate": {
		"*.dll":                              true,
		"AutoMonitorControl.dll":             false,
		"Jointsky.Communicate.Component.dll": false,
		"SendRedisPackage.dll":               false,
		"Shell/":                             true,
		"runtimes/":                          true,
		"Nssm/":                              true,
		"MonitorConfig/MongoDBConfig.xml":    true,
		"MonitorConfig/RedisConfig.xml":      true,
	},
	"Communicate-noise": {
		"*.dll":          true,
		"Shell/":         true,
		"runtimes/":      true,
		"Nssm/":          true,
		"MonitorConfig/": true,
	},
	"DataImportServer": {
		"lib/":                        true,
		"conf/application-system.yml": true,
	},
	"DataImportServer-noise": {
		"lib/":                        true,
		"conf/application-system.yml": true,
	},
	"DataImportServerYD": {
		"lib/":                        true,
		"conf/application-system.yml": true,
	},
	"jdex": {
		"share/java/*": true,
	},
	"jointframe-datax-console": {
		"lib/": true,
	},
	"jointframe-streams-data-retrigger": {
		"lib/": true,
	},
	"jointframe-streams-poll": {
		"lib/": true,
	},
	"kafka-streams-jdex-mongo2sql": {
		"lib/": true,
	},
	"kafka-streams-jdex-split": {
		"lib/": true,
	},
	"kafka-streams-jdex-split-rmdbs": {
		"lib/": true,
	},
	"AutoMonitorInterface": {
		"lib/": true,
	},
}

/*
amonline.4.2.0.log
AutoMonitorManager.log
AutoMonitorManagerQY.log
AutoMonitorRefreshServer.log
AutoMonitorServer.log
AutoMonitorInterface.log
Communicate.log
Communicate-noise.log
DataImportServer-3.0.8.0.log
DataImportServer-noise-3.0.8.0.log
DataImportServerYD-3.0.8.0.log
jdex.log
jointcloud.4.2.0.log
jointframe-datax-console.log
jointframe-streams-data-Retrigger.log
jointframe-streams-poll.log
jointos.4.2.0.log
kafka-streams-jdex-mongo2sql.log
kafka-streams-jdex-split.log
kafka-streams-jdex-split-rmdbs.log
*/

// ExcludedPatterns 定义需要排除的文件名正则表达式列表
// 注意: 所有表达式已包含^和$锚点确保完全匹配
var ExcludedPatterns = []string{
	"^config\\.env\\.js$",        // 配置文件
	"^application\\.yml$",        // 应用配置文件
	"^web\\.xml$",                // web配置文件
	"^CommunicatesConfig\\.xml$", // 通讯配置文件
	//"^.*\\.doc$",                 // 文档文件
	//"^.*\\.docx$",                // 文档文件
	"^.*\\.md$",                  // 文档文件
	"^AutoMonitorControl\\.dll$", // 反控组件
	"^Jointsky\\.Communicate\\.Component\\.dll$", // 通讯组件
	"^SendRedisPackage\\.dll$",                   // 发送redis包组件
}

// 预编译的排除模式正则表达式
var compiledExcludePatterns []*regexp.Regexp

// init 初始化排除模式正则表达式
func init() {
	for _, pattern := range ExcludedPatterns {
		// 直接编译正则表达式
		re, err := regexp.Compile(pattern)
		if err == nil {
			compiledExcludePatterns = append(compiledExcludePatterns, re)
		} else {
			// 记录无效的正则表达式模式
			log.Printf("警告: 无效的排除正则表达式 '%s': %v\n", pattern, err)
		}
	}
}

// IsExcluded 判断文件名是否匹配任何排除模式
func IsExcluded(filename string) bool {
	for _, re := range compiledExcludePatterns {
		if re.MatchString(filename) {
			return true
		}
	}
	return false
}
